package com.xingchen.feima.star.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingchen.feima.common.utils.R;
import com.xingchen.feima.star.entity.Range;
import com.xingchen.feima.star.service.RangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 范围（设置燃耗范围或者初始富集度范围） 前端控制器
 * </p>
 *
 * @author yangxc
 * @since 2025-02-16
 */
@Api(tags = "范围管理")
@RestController
@RequestMapping("/star/range")
public class RangeController {

    @Autowired
    private RangeService rangeService;


    @ApiOperation(value = "根据是低燃耗表还是高燃耗表、是燃耗还是初始铀富集度来获取燃耗范围列表")
    @GetMapping("/getRangeList/{isLowBurnupOrHighBurnup}/{isBurnupOrInitialUraniumEnrichment}")
    public R getRangeList(
            @ApiParam(name = "isLowBurnupOrHighBurnup", value = "是低燃耗还是高燃耗", required = true)
            @PathVariable Integer isLowBurnupOrHighBurnup,
            @ApiParam(name = "isBurnupOrInitialUraniumEnrichment", value = "是燃耗还是初始铀富集度", required = true)
            @PathVariable Integer isBurnupOrInitialUraniumEnrichment){

        List<Range> rangeList = rangeService.list(new QueryWrapper<Range>()
                .eq("is_low_burnup_or_high_burnup", isLowBurnupOrHighBurnup)
                .eq("is_burnup_or_initial_uranium_enrichment", isBurnupOrInitialUraniumEnrichment)
        );
        return R.ok().data("items", rangeList);
    }


    @ApiOperation(value = "新增范围")
    @PostMapping("")
    public R add(
            @ApiParam(name = "range", value = "范围对象", required = true)
            @RequestBody Range range){

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(range.getLowValue().toString());
        if (range.getIsGetLowValue() == 0) {
            stringBuilder.append("<");
        } else {
            stringBuilder.append("≤");
        }

        if (range.getIsBurnupOrInitialUraniumEnrichment() == 0) {
            stringBuilder.append("B");
        } else {
            stringBuilder.append("E");
        }

        if (range.getIsGetHighValue() == 0) {
            stringBuilder.append("<");
        } else {
            stringBuilder.append("≤");
        }
        stringBuilder.append(range.getHighValue().toString());
        String rangeDescribe = stringBuilder.toString();
        range.setRangeDescribe(rangeDescribe);

        rangeService.save(range);
        return R.ok().message("添加成功");
    }

    @ApiOperation(value = "根据id查询范围")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id", value = "范围id", required = true)
            @PathVariable Long id){

        Range range = rangeService.getById(id);
        return R.ok().data("items", range);
    }

    @ApiOperation(value = "根据id修改范围")
    @PutMapping("")
    public R updateById(
            @ApiParam(name = "range", value = "范围对象", required = true)
            @RequestBody Range range){

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(range.getLowValue().toString());
        if (range.getIsGetLowValue() == 0) {
            stringBuilder.append("<");
        } else {
            stringBuilder.append("≤");
        }

        if (range.getIsBurnupOrInitialUraniumEnrichment() == 0) {
            stringBuilder.append("B");
        } else {
            stringBuilder.append("E");
        }

        if (range.getIsGetHighValue() == 0) {
            stringBuilder.append("<");
        } else {
            stringBuilder.append("≤");
        }
        stringBuilder.append(range.getHighValue().toString());
        String rangeDescribe = stringBuilder.toString();
        range.setRangeDescribe(rangeDescribe);

        rangeService.updateById(range);
        return R.ok().message("修改成功");
    }

    @ApiOperation(value = "根据id删除范围")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "范围id", required = true)
            @PathVariable Long id){

        rangeService.removeById(id);
        return R.ok().message("删除成功");
    }

    @ApiOperation("根据id列表批量删除范围")
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList", value = "id列表", required = true)
            @RequestBody List<Long> idList) {

        rangeService.removeByIds(idList);
        return R.ok().message("批量删除成功");
    }
}



